package com.cn.zengzeng.spbzengzeng.utils;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.*;


/**
 * @Description:
 * @Author: zhangzengzeng
 * @Date: 2021/7/15 17:36
 */
public class Base64Util {
    
    public static String fileToBase64(String filepath, String filename) {
        String fileName = filepath + filename; // 源文件
        String strBase64 = null;
        try(
            InputStream in = new FileInputStream(fileName)
        ) {
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            // 将文件中的内容读入到数组中
            in.read(bytes);
            // strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
            strBase64 = encode(bytes); // 将字节流数组转换为字符串
        }catch (IOException fe) {
            fe.printStackTrace();
        }
        return strBase64;
    }

    public static String fileToBase64(String filepathame)  {
        String fileName = filepathame; // 源文件
        String strBase64 = null;
        try(
            InputStream in = new FileInputStream(fileName)
        ) {
            // in.available()返回文件的字节长度
            byte[] bytes = new byte[in.available()];
            // 将文件中的内容读入到数组中
            in.read(bytes);
            // strBase64 = new BASE64Encoder().encode(bytes); // 将字节流数组转换为字符串
            strBase64 = encode(bytes); // 将字节流数组转换为字符串
        } catch (IOException fe) {
            fe.printStackTrace();
        }
        return strBase64;
    }

    public static void base64ToFile(String strBase64,String filepath,String filename)  {
        String fileName = filepath + filename;// 生成的新文件
        // 解码，然后将字节转换为文件
        byte[] bytes = decode(strBase64.trim());
        try (
            ByteArrayInputStream in = new ByteArrayInputStream(bytes);
            FileOutputStream out = new FileOutputStream(fileName)
        ) {
            // 将字符串转换为byte数组
            byte[] buffer = new byte[1024];
            int byteread = 0;
            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread); // 文件写操作
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    /**
     * 编码
     *
     * @param bstr
     * @return String
     */
    public static String encode(byte[] bstr) {
        return new BASE64Encoder().encode(bstr);
    }

    /**
     * 解码
     *
     * @param str
     * @return string
     */
    public static byte[] decode(String str) {
        byte[] bt = null;
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            bt = decoder.decodeBuffer(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bt;
    }


    public static void main(String[] args) {
        //将文件流化
        System.out.println(fileToBase64("E:\\360Downloads\\Software\\禁网\\", "荷塘月色.mp3"));
        //将流化文件重新生成文件
        base64ToFile(fileToBase64("E:\\360Downloads\\Software\\禁网\\", "荷塘月色.mp3"), "E:\\", "2.mp3");
    }
}

